package com.example.litterpainterdemo.utils

import android.animation.Animator
import android.animation.AnimatorSet
import android.animation.ObjectAnimator
import android.view.View
import android.view.animation.AlphaAnimation
import android.view.animation.Animation
import android.view.animation.Animation.AnimationListener
import android.view.animation.AnimationSet
import android.view.animation.AnimationUtils
import android.view.animation.BounceInterpolator
import android.view.animation.Interpolator
import android.view.animation.LinearInterpolator
import android.view.animation.ScaleAnimation

object AnimUtils {

    //缩放加渐变的动画
    fun addScaleAndAlphaAnim(
        target: View,
        fromX:Float = 0.4f, toX:Float = 1f,
        fromY:Float = 0.4f, toY:Float = 1f,
        fromAlpha: Float = 0.1f, toAlpha:Float = 1f,
        duration:Long = 1500,
        interpolator: Interpolator = BounceInterpolator(),
        onStart:()->Unit = {},
        onEnd:()->Unit = {},
        onRepeat:()->Unit = {}
        ){
        val scaleAnim = ScaleAnimation(
            fromX, toX,
            fromY, toY,
            Animation.RELATIVE_TO_SELF,0.5f,
            Animation.RELATIVE_TO_SELF,0.5f
        )

        val alphaAnim = AlphaAnimation(fromAlpha, toAlpha)


        //AnimationSet用于将多个动画组合在一起执行
        //shareInterpolator----用于指定是否共享子动画的插值器（interpolator）。
        AnimationSet(true).apply {
            this.duration = duration
            this.interpolator = interpolator
            addAnimation(scaleAnim)
            addAnimation(alphaAnim)

            setAnimationListener(object :Animation.AnimationListener{
                override fun onAnimationStart(animation: Animation?) {
                    onStart()
                }

                override fun onAnimationEnd(animation: Animation?) {
                   onEnd()
                }

                override fun onAnimationRepeat(animation: Animation?) {
                   onRepeat()
                }

            })

            target.startAnimation(this)
        }
    }

    //添加旋转加渐变动画
    fun addRotateAndAlphaAnimation(
        target: View,
        fromDegree:Float = 180f,toDegree:Float = 360f,
        fromAlpha:Float = 0f,toAlpha: Float = 1f,
        duration: Long = 1000,
        onStart:()->Unit = {},
        onEnd:()->Unit = {},
        onCancel:()->Unit = {},
        onRepeat:()->Unit = {}
    ){
        val rotateAnim = ObjectAnimator.ofFloat(target,"rotationX",fromDegree,toDegree)
        val alphaAnim = ObjectAnimator.ofFloat(target,"alpha",fromAlpha,toAlpha)

        //用于将多个动画组合在一起执行
        AnimatorSet().apply {
            this.duration = duration

            //playSequentially(rotateAnim,alphaAnim) 按顺序播放
            //playTogether一起播放
            playTogether(rotateAnim,alphaAnim)

            addListener(object :Animator.AnimatorListener{
                override fun onAnimationStart(animation: Animator) {
                    onStart()
                }

                override fun onAnimationEnd(animation: Animator) {
                    onEnd()
                }

                override fun onAnimationCancel(animation: Animator) {
                    onCancel()
                }

                override fun onAnimationRepeat(animation: Animator) {
                    onRepeat()
                }

            })
            start()
        }
    }

}